import * as THREE from "three";

// 初始化地图浮动效果
function initMapFloat (camera, mapModel,width,height) {
    // 节流函数
    const throttleChange = throttle(rayMonitor, 5, false);
    // 监听鼠标移动事件
    addEventListener("mousemove", (e) => {
        throttleChange(e, camera, mapModel,width,height);
    });

}

// 节流函数
function throttle (func, limit) {
    let inThrottle;
    return function () {
        const args = arguments;
        const context = this;
        if (!inThrottle) {
            func.apply(context, args);
            inThrottle = true;
            setTimeout(() => (inThrottle = false), limit);
        }
    };
}

// 射线监测当前鼠标移动点是否有地图模型
function rayMonitor (e, camera, mapModel,width,height) {
    const px = e.offsetX;
    const py = e.offsetY;
    // 屏幕坐标转为标准设备坐标
    const x = (px / width) * 2 - 1;
    const y = -(py / height) * 2 + 1;
    // 创建射线
    const raycaster = new THREE.Raycaster();
    // 设置射线参数
    raycaster.setFromCamera(new THREE.Vector2(x, y), camera);
    // 射线交叉计算拾取模型
    let intersects = raycaster.intersectObjects(mapModel.children);
    // 检测结果过滤边线
    intersects = intersects.filter(function (intersect) {
        return intersect.object.name !== "边线" && intersect.object.name !== "光圈" && intersect.object.name !== "棱柱";
    });
    // 先停止所有动画
    mapModel.children.forEach((item) => {
        if (item.userData.animationTimer) {
            clearInterval(item.userData.animationTimer);
            item.userData.animationTimer = null;
        }
        item.userData.animationActive = false;
    });
    // 射线选中模型时
    if (intersects.length > 0) {
        const obj = intersects[0].object.parent;
        obj.userData.animationActive = true;
        mapModel.children.map((item) => {
            if (item.userData.animationActive) {
                // 添加一个白色的发射光呈现高亮效果
                item.traverse(obj => {
                    if (obj.type === 'Mesh' && obj.name === '') {
                        obj.material[0].emissive = new THREE.Color("#fff");
                    }
                })
                item.userData.animationTimer = setInterval(() => {
                    if (item.position.z <= 4) {
                        item.position.z += 0.3;
                    } else {
                        clearInterval(item.userData.animationTimer);
                        item.userData.animationTimer = null;
                    }
                }, 5);
            } else {
                // 默认重置为黑色的发射光
                item.traverse(obj => {
                    if (obj.type === 'Mesh' && obj.name === '') {
                        obj.material[0].emissive = new THREE.Color("#000");
                    }
                })
                item.userData.animationTimer = setInterval(() => {
                    if (item.position.z > 0) {
                        item.position.z -= 0.1;
                    } else {
                        clearInterval(item.userData.animationTimer);
                        item.userData.animationTimer = null;
                    }
                }, 5);
            }
        });
    } else {
        mapModel.children.forEach((item) => {
            item.userData.animationTimer = setInterval(() => {
                item.traverse(obj => {
                    if (obj.type === 'Mesh' && obj.name === '') {
                        obj.material[0].emissive = new THREE.Color("#000");
                    }
                })

                if (item.position.z > 0) {
                    item.position.z -= 0.1;
                } else {
                    clearInterval(item.userData.animationTimer);
                    item.userData.animationTimer = null;
                }
            }, 5);
        });
    }
}

export { initMapFloat };
